package cn.zhaoyuening.hnistalumnirecord.web.interceptor;

import cn.zhaoyuening.hnistalumnirecord.domain.AdminUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by zhao on 17-1-26.
 */
public class AdminLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object adminLoginUser = session.getAttribute("adminLoginUser");
        if (adminLoginUser!=null&&adminLoginUser instanceof AdminUser){
            //用户处于登录状态 允许访问后台
            return true;
        }
        //用户不处于登录状态 禁止登录后台
        response.sendRedirect("/hello/index.do");

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
